package com.vito.dcs.water.action;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.DataGrid;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.tools.I18nResMsg;
import com.vito.base.util.Validata;
import com.vito.dcs.water.device.model.WaterStationModel;
import com.vito.dcs.water.device.service.WaterStationServie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @program: WaterStationAction
 * @description: 水稳拌合站信息表请求控制器
 * @author: rongr
 * @create: 2019/04/11
 */
@Controller
@RequestMapping("/dcs/water/station")
public class WaterStationAction extends BaseAction {

    private static final Logger log = LoggerFactory.getLogger(WaterStationAction.class);

    @Reference
    private WaterStationServie waterStationServie;

    /**
     * 保存waterStationModel对象
     *
     * @Param: [waterStationModel]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/saveWaterStationModel")
    @ResponseBody
    public Result saveWaterStationModel(WaterStationModel waterStationModel) {
        Result res = new Result();
        UserModel obj = SessionMap.getObj();
        if (obj == null || Validata.isNullOrEmpty(obj.getUserId())) {
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
            return res;
        }
        try {
            waterStationModel.setUpdateUser(obj.getUserId());
            waterStationServie.saveWaterStation(waterStationModel);
            res.setCode(Result.CODE_OK);
            res.setMsg(I18nResMsg.SAVESUC());
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg(I18nResMsg.SAVEERR());
            log.error(I18nResMsg.SAVEERR(), e);
        }
        return res;
    }

    /**
     * 修改waterStationModel对象
     *
     * @Param: [waterStationModel]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/updateWaterStationModel")
    @ResponseBody
    public Result updateWaterStationModel(WaterStationModel waterStationModel) {
        Result res = new Result();
        UserModel obj = SessionMap.getObj();
        if (obj == null || Validata.isNullOrEmpty(obj.getUserId())) {
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
            return res;
        }
        try {
            waterStationModel.setUpdateUser(obj.getUserId());
            waterStationServie.updateWaterStation(waterStationModel);
            res.setCode(Result.CODE_OK);
            res.setMsg(I18nResMsg.SAVESUC());
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg(I18nResMsg.SAVEERR());
            log.error(I18nResMsg.SAVEERR(), e);
        }
        return res;
    }

    /**
     * 分页查询WaterStationModel对象
     *
     * @Param: [map]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/queryStationByPage")
    @ResponseBody
    public Result queryStationByPage(WaterStationModel waterStationModel) {
        Result res = new Result();
        try {
            DataGrid<WaterStationModel> queryRes = waterStationServie.queryWaterStationByPage(waterStationModel);
            res.setCode(Result.CODE_OK);
            res.setData(queryRes);
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg("查询失败");
        }
        return res;
    }

    /**
     * 删除WaterStationModel对象
     *
     * @Param: [waterStationModel]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/deleteStationModel")
    @ResponseBody
    public Result deleteStationModel(WaterStationModel waterStationModel) {
        Result res = new Result();
        try {
            waterStationServie.deleteStationModel(waterStationModel);
            res.setCode(Result.CODE_OK);
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg("删除失败");
        }
        return res;
    }

    /**
     * 根據水穩拌合站ID查看是否存在攪拌機
     * @param blenderId
     * @return
     */
    @RequestMapping("queryWaterStationDel")
    @ResponseBody
    public Result queryWaterStationDel(String blenderId){
        Result rs = new Result();
        try{
            if(blenderId == null){
                blenderId = "0";
            }
            rs.setData(waterStationServie.queryWaterPoreBlenderId(blenderId));
            rs.setCode(Result.CODE_OK);
        }catch(Exception ex){
            log.error("查询失败",ex);
            rs.setCode(Result.CODE_ERROR);
            rs.setMsg(ResMsg.EXECERR);
        }
        return rs;
    }

}

